/**
 *主体/债项评级组件
 */
import React, { Component } from 'react';
import _ from 'lodash';
import { connect } from 'dva';
import PropTypes from 'prop-types';
import { QuestionCircleOutlined } from '@ant-design/icons';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Tooltip } from 'antd';
import { SelectC, FormItemLayout, NoLableLayout } from '../..';
import styles from '../../../css/common/form.less';

class RateRangeC extends Component {
  // props默认值
  static defaultProps = {
    title: undefined, // 名称，默认为空
    helper: '', // 下拉框提示信息，默认无提示信息
    dataRange: ['AAA', 'AA+', 'AA', 'AA-', 'A+', 'A', 'A-', 'BBB+', 'BBB', 'BBB-', 'BB+', 'BB', 'BB-', 'B+', 'B', 'B-', 'CCC+', 'CCC', 'CCC-', 'CC', 'C', 'D'], // dataSource数据包含等级
    disabled: false,
    onRef: () => {},
    onChange: () => {},
    formItemLayout: FormItemLayout,
    labalWrap: false, // 默认title不换行
  };

  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    // 父组件对本组件的引用
    this.props.onRef(this);
  }

  // 设置选项值
  setDataource = (dataRange) => {
    const {
      globalM: {
        dataDictionary: { RATERANGESTART_END = [] },
      },
    } = this.props;
    const arr = [];
    RATERANGESTART_END.map((item, index) => {
      dataRange.map((itemData) => {
        if (itemData == item.value) {
          arr.push({
            value: item.code,
            title: item.value,
          });
        }
      });
    });
    return arr;
  };

  // 非初始化状态下更改下拉选项(供父组件调用)
  setSelect = (value) => {
    console.log(value, '123', this.props.dataIndex);
    const valueF = {};
    valueF[this.props.dataIndex] = value;
    console.log(valueF);
    this.props.form.setFieldsValue(valueF);
  };

  render() {
    const { labalWrap, styleRow, form, title, dataIndex, style, initialValue, helper, formItemLayout, dataRange, disabled } = this.props;
    const dataSource = this.setDataource(dataRange);
    const rootStyle = { alignItems: 'center', display: 'flex' };
    return (
      <Form.Item
        className={labalWrap ? 'labalWrap' : ''}
        style={style}
        labelCol={title ? formItemLayout.labelCol : NoLableLayout.labelCol}
        wrapperCol={title ? formItemLayout.wrapperCol : NoLableLayout.wrapperCol}
        label={
          title ? (
            helper ? (
              <span className={this.props.rules?.find((item) => item.required) ? styles.lableRequire : ''}>
                {title}&nbsp;
                <Tooltip title={helper}>
                  <QuestionCircleOutlined />
                </Tooltip>
              </span>
            ) : (
              <span className={this.props.rules?.find((item) => item.required) ? styles.lableRequire : ''}>{title}</span>
            )
          ) : (
            ''
          )
        }
      >
        {
          <div style={{ ...rootStyle }}>
            <Form.Item style={{ width: '100%', marginBottom: '0 ', color: 'red' }}>
              <SelectC
                rules={this.props.rules}
                form={form}
                dataSource={dataSource}
                dataIndex={dataIndex}
                initialValue={initialValue}
                allowClear={true}
                style={styleRow ? { marginBottom: '0', ...styleRow } : { marginBottom: '0' }}
                disabled={disabled}
                onRef={(ref) => this.setState({ SelectC: ref })}
                onChange={() => this.props.onChange()}
                positionBody={true}
              />
            </Form.Item>
          </div>
        }
      </Form.Item>
    );
  }
}

// props类型
RateRangeC.propsTypes = {
  form: PropTypes.object, // form对象
  title: PropTypes.string, // 显示名称
  dataIndex: PropTypes.array, // Form识别的Item ID
  initialValue: PropTypes.array, // 初始默认值
  onChange: PropTypes.func, // 开始等级或结束等级变化时的回调
  helper: PropTypes.string, // 提示帮助信息
  dataRange: PropTypes.array, // 数据选择包含等级[开始等级, 结束等级]
  onRef: PropTypes.func, // 本组件的引用（可用来父组件调用内部方法）
  disabled: PropTypes.bool,
  formItemLayout: PropTypes.object, // 布局
};

export default connect(({ globalM }) => ({ globalM }))(RateRangeC);
